﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Services.Providers;
using System.Data.Services;
using System.IO;

namespace Skynet.ServiceImplementation
{
    public class SkynetSupportService 
    {
        private IList<ServiceContracts.DataContracts.SupportInfo> _Supports;
        private IList<ServiceContracts.DataContracts.PhotoInfo> _Photos;

        public SkynetSupportService()
        {
            _Supports = new List<ServiceContracts.DataContracts.SupportInfo>();
            this._Supports.Add(new ServiceContracts.DataContracts.SupportInfo() { Id = 1, Message = "Primer servidor" });
            this._Supports.Add(new ServiceContracts.DataContracts.SupportInfo() { Id = 2, Message = "Segundo servidor" });
            this._Supports.Add(new ServiceContracts.DataContracts.SupportInfo() { Id = 3, Message = "Tercer servidor" });

            this._Photos = new List<ServiceContracts.DataContracts.PhotoInfo>();
            this._Photos.Add(new ServiceContracts.DataContracts.PhotoInfo() { Id  =1, path = "black_widow_iron_man_2-wide.jpg" });
            this._Photos.Add(new ServiceContracts.DataContracts.PhotoInfo() { Id =2,  path = "black-widow-scarlett-johansson-3.jpg" });            
        }
        public IQueryable<ServiceContracts.DataContracts.SupportInfo> Supports
        {
            get {
                return this._Supports.AsQueryable<ServiceContracts.DataContracts.SupportInfo>(); 
            }
        }
        public IQueryable<ServiceContracts.DataContracts.PhotoInfo> Photos
        {
            get {
                return this._Photos.AsQueryable();
            }
        }
     
    }
    public class SkynetStreamProvider : IDataServiceStreamProvider 
    {

        public void DeleteStream(object entity, System.Data.Services.DataServiceOperationContext operationContext)
        {
         
        }

        public System.IO.Stream GetReadStream(object entity, string etag, bool? checkETagForEquality, System.Data.Services.DataServiceOperationContext operationContext)
        {
            if (checkETagForEquality != null)
            {                
                throw new DataServiceException(400, "This sample service does not support the ETag header for a media resource.");
            }

            var image = entity as Skynet.ServiceContracts.DataContracts.PhotoInfo;
            if (image == null)
            {
                throw new DataServiceException(500, "Internal Server Error.");
            }

            // Build the full path to the stored image file, which includes the entity key. 
            string fullImageFilePath = @"C:\Users\gregory\Pictures\desktop\" + image.path;

            if (!File.Exists(fullImageFilePath))
            {
                throw new DataServiceException(500, "The image file could not be found.");
            }

            // Return a stream that contains the requested file. 
            return new FileStream(fullImageFilePath, FileMode.Open); 
        }

        public Uri GetReadStreamUri(object entity, System.Data.Services.DataServiceOperationContext operationContext)
        {
            return null;
        }

        public string GetStreamContentType(object entity, System.Data.Services.DataServiceOperationContext operationContext)
        {            
            return "image/jpeg"; 
        }

        public string GetStreamETag(object entity, System.Data.Services.DataServiceOperationContext operationContext)
        {
            return null;   
        }

        public System.IO.Stream GetWriteStream(object entity, string etag, bool? checkETagForEquality, System.Data.Services.DataServiceOperationContext operationContext)
        {
            return null;
        }

        public string ResolveType(string entitySetName, System.Data.Services.DataServiceOperationContext operationContext)
        {
            if (entitySetName == "PhotoInfo")
            {
                return "Skynet.ServiceContracts.DataContracts.PhotoInfo";
            }
            else
            {             
                return null;
            } 
        }

        public int StreamBufferSize
        {
            get { return 64000; }
        }
    }
}
